from bank.User import User


class Bank:

    def __init__(self, user_dict, bank_name,max_user = 10):
        self.user_dict = user_dict
        self.bank_name = bank_name
        self.max_user = max_user

    def add_user(self,_user):
        for (user_id,account) in self.user_dict.items():
            if account.name == _user.name:
                return 2
            if len(self.user_dict) >= self.max_user:
                return 3
        self.user_dict[_user.account] = _user
        return 1


    def __str__(self):
        user_info = ", ".join([f"{account}" for user_id, account in self.user_dict.items()])
        return "银行名称:{}\n用户数量:{}\n用户:\n{}".format(self.bank_name,len(self.user_dict),user_info)

    def deposit(self, _account, money):
        account = self.user_dict.get(_account)
        if account is None:
            return 2
        account.money += money
        return 1


    def withdraw(self, _account, money):
        account = self.user_dict.get(_account)
        if account is None:
            return 3
        if account.money < money:
            return 2
        account.money -= money
        return 1

    def transfer(self, old_account, new_account, money):
        # 获取账户信息
        old_acc = self.user_dict.get(old_account)
        new_acc = self.user_dict.get(new_account)

        if old_acc is None or new_acc is None:
            return 3

        # 处理相同账户的情况
        if old_account == new_account:
            return 4

        # 检查余额是否足够
        if old_acc.money < money:
            return 2

        # 执行转账操作
        old_acc.money -= money
        new_acc.money += money

        return 1

    def query(self, account):
        _account = self.user_dict.get(account)
        if _account is None:
            return 2
        else:
            return _account


